#ifndef AVALON_TASKWRITER_HPP
#define AVALON_TASKWRITER_HPP
#include <vector>
#include "Avalon/Base/Handle.hpp"
#include "Avalon/IO/Writer.hpp"

namespace Avalon {
namespace IO {

  /*! \class TaskWriter
   *  \brief Uses a Task to write data.
   *  \author Kamal Mansouri <kranar@hotmail.com>
   */
  class TaskWriter : public Writer {
    public:

      //! Constructs a TaskWriter.
      /*!
        \param destination The destination to write to.
      */
      TaskWriter(Carry<Writer> destination);

      virtual ~TaskWriter();

      virtual Threading::Async<void>::Ptr Write(const void* data, int size);

      virtual Threading::Async<void>::Ptr Write(const Buffer& data);

    private:
      class WriterTask;
      boost::mutex m_mutex;
      std::vector<std::pair<Buffer, Threading::AsyncOwner<void>::Ptr> >
        m_pendingWrites;
      Handle<Writer> m_destination;
      boost::scoped_ptr<WriterTask> m_task;
  };
}
}

#endif // AVALON_TASKWRITER_HPP
